package com.zyh.domain;

import java.util.Arrays;

public class MyArrayUtilImpl extends MyArrayUtil{

    //查找数组元素
    @Override
    public void findAll(int[] arr) {
        System.out.println(Arrays.toString(arr));
    }

    //二分查找
    @Override
    public void findIndex(int[] arr, int value) {
        int min=0,max=arr.length-1,mid;
        while (min<=max){
            mid=(min+max)/2;
            if (value>arr[mid]){
                min=mid+1;
            }
            else if (value<arr[mid]){
                max=mid-1;
            }
            else if (value==arr[mid]){
                System.out.println(value+"在数组中的索引是："+mid);
                break;
            }
        }
        if (min>max){
            System.out.println(value+"在数组中不存在");
        }
    }

    //数组的反转
    @Override
    public int[] fanZhuanShuZu(int[] arr) {
        int start,end;
        for (start=0,end=arr.length-1;start<=end;start++,end--){
            int temp=arr[start];
            arr[start]=arr[end];
            arr[end]=temp;
        }
        return arr;
    }

    //删除数组
    @Override
    public int[] deleteArray(int[] arr, int valueIndex) {
        int[] newArr=new int[arr.length-1];
        for (int i = 0; i < valueIndex; i++) {
            newArr[i]=arr[i];
        }
        for (int i = valueIndex+1; i < arr.length; i++) {
            newArr[i-1]=arr[i];
        }
        return newArr;
    }

    //添加元素
    @Override
    public int[] addArray(int[] arr, int valueIndex,int value) {
        int[] newArr=new int[arr.length+1];
        for (int i = 0; i < valueIndex; i++) {
            newArr[i]=arr[i];
        }
        for (int i=valueIndex;i<arr.length;i++){
            newArr[i+1]=arr[i];
        }
        newArr[valueIndex]=value;
        return newArr;
    }

    @Override
    public boolean isFullArray(int[] arr, int index) {
        return false;
    }
}
